package com.cw.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.pojo.TbItem;
import com.cw.pojo.TbOrderItem;
import com.cw.service.CartService;
import com.cw.service.ItemService;
import com.cw.vo.Cart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class CartServiceImpl implements CartService {

    @Reference
    private ItemService itemService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Cart> addCart(List<Cart> cartList, Long itemId, Integer num) {
        //根据itemId查查询item对象
        TbItem item = itemService.getItemById(itemId);
        //获取商家id
        String sellerId = item.getSellerId();
        //1.如果没有原来商家的购物车数据,创建购物车
        Cart cart = getCartBySellerId(sellerId, cartList);
        if(cart == null){
            cart = new Cart();
            cart.setSellerId(sellerId);
            TbOrderItem orderItem = cartOrderItem(item, num);
            List<TbOrderItem> orderItemList = new ArrayList<>();
            orderItemList.add(orderItem);
            cart.setOrderItemList(orderItemList);
            cartList.add(cart);

        }else{
            //2.如果有原来的商家购物车数据,
            TbOrderItem orderItem = getOrderItemId(cart.getOrderItemList(), itemId);
            //2.1但是没买过当前商品,拿来购物车 追加一条
            if(orderItem == null){
                orderItem = cartOrderItem(item, num);
                cart.getOrderItemList().add(orderItem);
            }else{
                //如果有原来的商家购物车数据,也有当前商品,只需要修改数量和价格
                orderItem.setNum(orderItem.getNum() + num);
                orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue() * orderItem.getNum()));
                if(orderItem.getNum() <= 0){
                    //如果减完数量后商品的购买的数量为0,我们要将当前商品从购物车那个明细列表移除
                    cart.getOrderItemList().remove(orderItem);
                }
                if (cart.getOrderItemList().size() <= 0){
                    //如果购物车明细只剩一个,我们刚好又把他移除了,那么这个明细就没有数据了,我们就将整个购物车从大的列表中移除
                    cartList.remove(cart);
                }
            }


        }
        //3.如果有原来的商家购物车数据,也有这件商品,我们只需要增加num,价格修改一下
        return cartList;
    }

    private Cart getCartBySellerId(String sellerId,List<Cart> cartList){
        return cartList.stream().filter(cart -> cart.getSellerId().equals(sellerId)).findFirst().orElse(null);

    }

    private  TbOrderItem cartOrderItem(TbItem item,Integer num){
        TbOrderItem orderItem = new TbOrderItem();
        orderItem.setNum(num);
        orderItem.setTitle(item.getTitle());
        orderItem.setPrice(item.getPrice());
        //总价格=当前价格*数量
        orderItem.setTotalFee(new BigDecimal(orderItem.getPrice().doubleValue() * num));
        orderItem.setPicPath(item.getImage());
        orderItem.setGoodsId(item.getGoodsId());
        orderItem.setItemId(item.getId());
        orderItem.setSellerId(item.getSellerId());

        return orderItem;
    }

    private TbOrderItem getOrderItemId(List<TbOrderItem> orderItemList,Long itemId){
        return orderItemList.stream().filter(orderItem -> orderItem.getItemId().equals(itemId)).findFirst().orElse(null);
    }

    @Override
    public List<Cart> findCartListFormRedis(String userName) {
        List<Cart> cartList = (List<Cart>) redisTemplate.boundHashOps("cartList").get(userName);
        return cartList == null ? new ArrayList<>() : cartList;
    }

    @Override
    public void addCarToRedis(List<Cart> cartList, String userName) {
        redisTemplate.boundHashOps("cartList").put(userName,cartList);
    }

    @Override
    public List<Cart> merageCart(List<Cart> cartListRedis, List<Cart> cartListCookie) {
        for (Cart cart : cartListCookie) {
            String sellerId = cart.getSellerId();
            List<TbOrderItem> orderItemList = cart.getOrderItemList();
            for (TbOrderItem orderItem : orderItemList) {

                cartListRedis = addCart(cartListRedis, orderItem.getItemId(), orderItem.getNum());
            }
        }
        return cartListRedis;
    }
}
